import { DataTypes, Model } from 'sequelize';

/** 用户表 */
export class User extends Model {}

/**
 * 初始化模型
 * @param {object} sequelize
 * @param {string} charset - 字符编码, utf8mb4
 */
export function initModels(sequelize, charset) {
  User.init(
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      username: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true,
        validate: {
          len: [8, 20],
        },
        comment: '用户名',
      },
      email: {
        type: DataTypes.STRING,
        unique: true,
        comment: '邮箱',
      },
      password: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '密码',
      },
    },
    { comment: '用户表', paranoid: true, sequelize: sequelize, charset, modelName: 'user' },
  );
}
